var buynowVue = new Vue({
	el: "body",
	data: {
		order: [],
		address: [],
		istrue: false,
		total: "",
		all: "",
		rid: "",
		isLoading:true
	},
	created: function() {
		init();
		_this = this
		loadPaying()
		loadAddress()
	},
	methods: {
		success: function() {
			successBuy(_this)
		},
		failure: function() {
			failueBuy(_this)
		}
	}

})

function loadPaying() {
	var currentUser = AV.User.current();
	var username = currentUser.attributes.username
	var user = new AV.Query('Order');
	user.equalTo("username", username);
	var pay = new AV.Query('Order');
	pay.equalTo("type", "paying");
	var query = AV.Query.and(pay, user);
	query.find().then(function(products) {
		removePayProduct(products[0].attributes.pid)
		var a = products[0].attributes.num
		_this.total = products[0].attributes.price
		_this.all = products[0].attributes.num
		_this.rid = products[0].id
		if(a > 3) {
			_this.istrue = true
			for(var i = 0; i < 3; i++) {
				var query = new AV.Query('Product');
				query.equalTo("pid", products[0].attributes.pid[i]);
				query.find().then(function(products) {
					_this.order.push(products[0]) 
					_this.isLoading=false
				}).catch(function(error) {
					console.log(JSON.stringify(error));
				});
			}
		} else {
			for(var i = 0; i < a; i++) {
				var query = new AV.Query('Product');
				query.equalTo("pid", products[0].attributes.pid[i]);
				query.find().then(function(products) {
					_this.order.push(products[0])
					_this.isLoading=false
				}).catch(function(error) {
					console.log(JSON.stringify(error));
				});
			}
		}
	}).catch(function(error) {
		console.log(JSON.stringify(error));
	});
}

function loadAddress() {
	var currentUser = AV.User.current();
	var username = currentUser.attributes.username
	var user = new AV.Query('Address');
	user.equalTo("user", username);
	var isDefault = new AV.Query('Address');
	isDefault.equalTo("isDefault", "default");
	var query = AV.Query.and(user, isDefault);
	query.find().then(function(products) {
		_this.address.push(products[0])
	}).catch(function(error) {
		console.log(JSON.stringify(error));
	});
}

function removePayProduct(a) {
	currentUser = AV.User.current();
	var username = currentUser.attributes.username
	for(var i = 0; i < a.length; i++) {
		console.log(username)
		var user = new AV.Query('Cart');
		user.equalTo("username", username);
		var pid = new AV.Query('Cart');
		pid.equalTo("pid", a[i]);
		var query = AV.Query.and(user, pid);
		query.find().then(function(products) {
			var todo = AV.Object.createWithoutData('Cart', products[0].id);
			todo.destroy().then(function(success) {}, function(error) {});
		}).catch(function(error) {
			console.log(JSON.stringify(error));
		});
	}
}

function successBuy(a) {
	var todo = AV.Object.createWithoutData('Order', a.rid);
	todo.set('type', 'payed');
	todo.save().then(
		function() {
			location.href = "order.html"
		}
	);
}

function failueBuy(a) {
	var todo = AV.Object.createWithoutData('Order', a.rid);
	todo.set('type', 'nopay');
	todo.save().then(
		function() {
			location.href = "order.html"
		}
	);
}